/**
 * 
 */
package ciprian.google.transport;

import ciprian.google.AuthenticatorActivity;
import ciprian.google.transport.NetworkUtilities.AsyncTaskParamType;
import android.content.Context;
import android.os.AsyncTask;

/**
 * @author Ciprian Ursu
 * 
 */
public class TransportAsyncTask extends AsyncTask<Context, Void, AsyncTaskParamType> {
    // use as param the ID of the calling activity
    // return the request status: OK, NOK, NETWORK, ...
    @Override
    protected AsyncTaskParamType doInBackground(Context... params) {
        for (Context context : params) {
            if (context == null) {
                // this is used when trying to send the data to server.
                // send the point to server
                NetworkUtilities.newPointsToSend();
                return null;
            } else if (context instanceof AuthenticatorActivity) {
                AsyncTaskParamType ret = new AsyncTaskParamType();
                ret.context = context;
                ret.result = NetworkUtilities.authenticate();

                return ret;
            }
        }
        throw new IllegalArgumentException();
    }

    @Override
    protected void onPostExecute(AsyncTaskParamType result) {
        if (result != null && result.context != null
                && result.context instanceof AuthenticatorActivity) {
            AuthenticatorActivity act = (AuthenticatorActivity) result.context;
            if (!act.isFinishing())
                act.onAuthenticationResult(result.result);
        }
    }
}
